*----------------------------------------------------------------------------------------------------------	* 
* RESEARCHERS:		EARN												   									*
* PROGRAMMED BY:	Linea Hasager																			*
* DESCRIPTION:		Heterogeneity analysis.																	*		
*					1) Load data 																			*
*					2) Heterogeneity analysis: RD estimates by subgroups									*
*					3) Heterogeneity analysis: tables with RD estimates										*
* CREATED:			Oct. 17, 2019																		   	*
* LAST MODIFIED:	May 25, 2022														       				*
*----------------------------------------------------------------------------------------------------------	*


clear 
set more off

// Directories
global in "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Input"
global output "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Output"
global temp "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Temp"


*--------------------------------------------------------------------------------------------------------	


*******************************
* 1) LOAD DATA
*******************************

use "${in}\Analysis_sample.dta", replace





*SHORTEN NAMES
rename employment_y_18_sum empl_y_18
rename loenmv_13_y_18_sum loen_y_18
rename non_complex_job_y18 ncomplex_job_y18



**********************************************
*	2) HETEROGENEITY ANALYSIS: RD ESTIMATION
**********************************************

*SPLIT SAMPLES, FIX BW AT BW FOR FULL POPULATION
foreach k in  latin_a urban female {
foreach j in 1 0 {
*OUTCOMES 
foreach var in loen_y_18 empl_y_18  complex_job_y18 ncomplex_job_y18 {
*FULL SAMPLE TO DETERMINE BANDWIDTH
qui rdrobust `var' day_nr,  kernel(tri) c(0) p(1) 
	
rdrobust `var' day_nr if `k'==`j',  kernel(tri) c(0) p(1) h(`e(h_l)') 
estadd scalar alpha=e(beta_p_l)[1,1]
eststo `var'_`k'`j'
estadd scalar bw = `e(h_l)'
}
}
}



*************************************************************************
*		3) HETEROGENEITY ANALYSIS: TABLE WITH RD ESTIMATES BY SUBGROUPS
*************************************************************************


local k latin_a
forval j=1/1 {
local txt_1 "Latin Alphabet"
local txt_0 "Other Alphabet"
local var_title "Alphabet of Mother Tongue"
esttab loen_y_18_`k'`j' empl_y_18_`k'`j' complex_job_y18_`k'`j' ncomplex_job_y18_`k'`j' using "${output}\table3.tex", replace se(%8.3fc) b(%8.3fc) nolabel nomtitle varlabel (RD_Estimate "`txt_`j''") noobs nolines nonum nonotes nogaps postfoot("") prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{4}{c}c}\toprule & Earnings & Employment & Complex Job & Non-Complex Job \\  & (1) & (2) & (3) & (4) \\") star(* 0.10 ** 0.05 *** 0.01)  posthead("\midrule & \multicolumn{4}{c}{\emph{Panel a. `var_title'}}\\") stats(alpha , fmt( %10.3fc) label("Mean of Untreated at Cutoff"))
}

local k latin_a
forval j=0/0 {
local txt_1 "Latin Alphabet"
local txt_0 "\shortstack{Other Alphabet \\ Than Latin}"
esttab loen_y_18_`k'`j' empl_y_18_`k'`j' complex_job_y18_`k'`j' ncomplex_job_y18_`k'`j' using "${output}\table3.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "`txt_`j''") noobs nolines nonum nonotes nogaps postfoot("") prehead("") star(* 0.10 ** 0.05 *** 0.01) posthead(" & \multicolumn{4}{c}{ }\\") stats(alpha, fmt(%10.3fc) label("Mean of Untreated at Cutoff"))
}



local k urban
forval j=1/1 {
local txt_1 "Urban Area"
local txt_0 "Rural Area"
local var_title "Initial Placement"
esttab loen_y_18_`k'`j' empl_y_18_`k'`j' complex_job_y18_`k'`j' ncomplex_job_y18_`k'`j' using "${output}\table3.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "`txt_`j''") noobs nolines nonum nonotes nogaps postfoot("") prehead("") star(* 0.10 ** 0.05 *** 0.01) posthead(" & \multicolumn{4}{c}{ }\\ & \multicolumn{4}{c}{\emph{Panel b. `var_title'}}\\") stats(alpha, fmt(%10.3fc) label("Mean of Untreated at Cutoff"))
}


local k urban
forval j=0/0 {
local txt_1 "Urban Area"
local txt_0 "Rural Area"
local var_title "Initial Placement"
esttab loen_y_18_`k'`j' empl_y_18_`k'`j' complex_job_y18_`k'`j' ncomplex_job_y18_`k'`j' using "${output}\table3.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "`txt_`j''") noobs nolines nonum nonotes nogaps postfoot("") prehead("") star(* 0.10 ** 0.05 *** 0.01) posthead(" & \multicolumn{4}{c}{ }\\") stats(alpha, fmt(%10.3fc) label("Mean of Untreated at Cutoff"))
}


local k female
forval j=1/1 {
local txt_1 "Female"
local txt_0 "Male"
local var_title "Gender"
esttab loen_y_18_`k'`j' empl_y_18_`k'`j' complex_job_y18_`k'`j' ncomplex_job_y18_`k'`j' using "${output}\table3.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "`txt_`j''") noobs nolines nonum nonotes nogaps postfoot("") prehead("") star(* 0.10 ** 0.05 *** 0.01) posthead(" & \multicolumn{4}{c}{ }\\ & \multicolumn{4}{c}{\emph{Panel c. `var_title'}}\\") stats(alpha, fmt( %10.3fc) label("Mean of Untreated at Cutoff"))
}


local k female
forval j=0/0 {
local txt_0 "Male"
local var_title "Gender"
esttab loen_y_18_`k'`j' empl_y_18_`k'`j' complex_job_y18_`k'`j' ncomplex_job_y18_`k'`j' using "${output}\table3.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "`txt_`j''") noobs nolines nonum nonotes nogaps postfoot("\bottomrule\end{tabular}}") prehead("") star(* 0.10 ** 0.05 *** 0.01) posthead(" & \multicolumn{4}{c}{ }\\") stats(alpha, fmt(%10.3fc) label("Mean of Untreated at Cutoff"))
}


